<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
		<title>谁让小球动起来</title>
		<style type="text/css">
		body{
			margin: 0;
		}
			#box {
				position: relative;
				height: 100vh;
			}
			.circle {
				border-radius: 50%;
				position: absolute;
				z-index: 2;
			}
		</style>
	</head>
	<body>
		<div id="box"></div>
	</body>
	<script type="text/JavaScript">
		window.onload=function(){
			for(let i=0;i<12;i++){
				new Circle()
			}
		}
		
		class Circle{
			constructor() {
			    this.r=this.randomNum(20,100);
				this.x=this.randomNum(0,window.innerWidth-200);
				this.y=this.randomNum(0,window.innerHeight-200);
				this.color=this.randomColor();
				this.createEl()
			}
			createEl(){
				this.circle=document.createElement('div');
				let {circle,r,x,y,color}=this;
				circle.classList.add('circle');
				circle.style.width=`${r*2}px`;
				circle.style.height=`${r*2}px`;
				circle.style.backgroundColor=`${color}`;
				circle.style.left=`${x}px`;
				circle.style.top=`${y}px`;
				box.appendChild(circle)
			}
			// 取指定范围随机数
			randomNum(min,max){
				let mun=Math.floor(Math.random() * (max - min + 1) + min);
				return mun
			}
			// 去随机的十六进制颜色：如#d51826
			randomColor(){
				var r = Math.floor(Math.random()*256);
				var g = Math.floor(Math.random()*256);
				var b = Math.floor(Math.random()*256);
				var color = '#'+r.toString(16)+g.toString(16)+b.toString(16);
				return color;
			}
		}
	</script>
</html>
